<?php

namespace app\controllers;

use app\models\Patient;
use Yii;
use app\models\Patientpower;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use app\commands\Service;

/**
 * PatientpowerController implements the CRUD actions for Patientpower model.
 */
class PatientpowerController extends Service
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Patientpower models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Patientpower::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Creates a new Patientpower model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionAutocreate()
    {
        $insert_count = 0;

        $sql = "select COLUMN_NAME,COLUMN_COMMENT from information_schema.COLUMNS where table_name = '" . Patient::tableName() . "' ";
        $command = Yii::$app->getDb()->createCommand($sql);
        $array = $command->queryAll();
        
        if(null != $array && count($array) > 0){
            foreach($array as $var){
                $var_name = trim($var['COLUMN_NAME']);
                $var_comment = trim($var['COLUMN_COMMENT']);
                $records = Patientpower::find()->where("var_name = '{$var_name}'")->all();
                if(null == $records || count($records) < 1){
                    $model = new Patientpower();
                    $model->var_name = $var_name;
                    $model->operate = '不显示';
                    $model->instruction = $var_comment;
                    if ($model->save()) {
                        $insert_count++ ;
                    }
                }

            }
        }
        
        if($insert_count == 0){
            return '<script>alert("没有任何patient的字段，新插入到patient_power表");window.location.href="'. Url::toRoute('patientpower/index') .'"</script>';
        } else {
            return '<script>alert("新插入字段'. $insert_count .'条");window.location.href="'. Url::toRoute('patientpower/index') .'"</script>';
        }
    }

    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    protected function findModel($id)
    {
        if (($model = Patientpower::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
